package com.yenlien.traceyun.business.service;


import com.yenlien.traceyun.business.dao.SupplierDao;
import com.yenlien.traceyun.business.dao.SupplierLogDao;
import com.yenlien.traceyun.business.entity.Supplier;
import com.yenlien.traceyun.business.entity.SupplierLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by LiuSha on 2020/6/8.
 */
@Service
public class SupplierService {
    @Autowired
    private SupplierDao supplierDao;
    @Autowired
    private SupplierLogDao supplierLogDao;

    public Page<Supplier> findByList(final Supplier supplier) {
        Pageable pageable = PageRequest.of(supplier.getPage(), supplier.getPageSize(), Sort.Direction.DESC, "createtime");
        return supplierDao.findAll(new Specification<Supplier>() {
            private static final long serialVersionUID = 1L;

            @Override
            public Predicate toPredicate(Root<Supplier> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Path<String> companyId = root.get("companyId");
                //构造查询条件
                List<Predicate> list = new ArrayList<>();
                List<Predicate> or = new ArrayList<>();
                if (!StringUtils.isEmpty(supplier.getCompanyId())) {
                    list.add(cb.equal(companyId, supplier.getCompanyId()));
                }
                if (!StringUtils.isEmpty(supplier.getArea())) {
                    list.add(cb.like(root.get("area"), "%" + supplier.getArea() + "%"));
                }
                Predicate[] predicate = new Predicate[list.size()];
                Predicate and = cb.and(list.toArray(predicate));
                if (!StringUtils.isEmpty(supplier.getQuery())) {
                    or.add(cb.like(root.get("supplierName"), "%" + supplier.getQuery() + "%"));
                    or.add(cb.like(root.get("userName"), "%" + supplier.getQuery() + "%"));
                    or.add(cb.like(root.get("phone"), "%" + supplier.getQuery() + "%"));
                } else {
                    return and;
                }
                Predicate[] pre_or = new Predicate[or.size()];
                Predicate ora = cb.or(or.toArray(pre_or));
                return query.where(and, ora).getRestriction();
            }
        }, pageable);
    }

    public Page<SupplierLog> findByListLog(final SupplierLog supplierLog) {
        Pageable pageable = PageRequest.of(supplierLog.getPage(), supplierLog.getPageSize(), Sort.Direction.DESC, "createTime");
        Page<SupplierLog> supplierLogs = supplierLogDao.findAll(new Specification<SupplierLog>() {
            private static final long serialVersionUID = 1L;

            @Override
            public Predicate toPredicate(Root<SupplierLog> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                //构造查询条件
                List<Predicate> list = new ArrayList<>();
                List<Predicate> or = new ArrayList<>();
                if (!StringUtils.isEmpty(supplierLog.getSupplierId())) {
                    list.add(cb.equal(root.get("supplierId"), supplierLog.getSupplierId()));
                }
                Predicate[] predicate = new Predicate[list.size()];
                Predicate and = cb.and(list.toArray(predicate));
                if (!StringUtils.isEmpty(supplierLog.getQuery())) {
                    or.add(cb.like(root.get("inputsName"), "%" + supplierLog.getQuery() + "%"));
                } else {
                    return and;
                }
                Predicate[] pre_or = new Predicate[or.size()];
                Predicate ora = cb.or(or.toArray(pre_or));
                return query.where(and, ora).getRestriction();
            }
        }, pageable);
        // 添加卡号和开户行
        for (SupplierLog supplierLog1 : supplierLogs.getContent()) {
            Supplier supplier = supplierDao.findById(supplierLog1.getSupplierId());
            supplierLog1.setBankCard(supplier.getBankCard());
            supplierLog1.setBankName(supplier.getBankName());
        }
        return supplierLogs;
    }


    public List<Supplier> findByAll(String companyId) {
        if (!StringUtils.isEmpty(companyId)) {
            return supplierDao.findByCompanyId(companyId);
        }
        return supplierDao.findAll();
    }

    public void save(Supplier supplier) {
        supplierDao.save(supplier);
    }

    public void update(Supplier supplier) {
        supplierDao.save(supplier);
    }

    public Supplier findById(String id) {
        return supplierDao.findById(id);
    }

    public void delete(String id) {
        supplierDao.deleteById(id);
    }

}
